package domain;

import java.awt.List;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.regex.*;


/**
 * Replaced by class LayerPropertyFile as per 12 december 2012.
 * Responsible for reading, converting and writing the general lithoclass 
 * and lithostratigraphy data 
 * pertinent to the model area under concern, typically from the 
 * lithoklasse.voleg and lithostratigrafie.voleg files within the .svp archive file.
 * The choice between lithoclass and lithostratigraphy is completely controlled by 
 * defining the proper input and output file names in Boss.java. Has method to write 
 * a 'lithoTable' to  csv file (so either lithoclasses or lithostratigraphy).
 * Uses regex from http://java.sun.com/developer/technicalArticles/releases/1.4regex/
 * @author kooijmanj1
 *
 */
public class Lithoklasse  {
	private static final String TAB="\u0009";
	private int ncols;
	private int nrows;
	private String[][] lithoTable;
	private String line;
	ArrayList<String> list = new ArrayList<String>();
	StringBuffer sb = new StringBuffer();

	/**
	 * lithoklasse.voleg en lithostratigrafie.voleg zijn onregelmatig geformatteerde
	 * ascii files met scheiding door tabs zodat in de afzonderlijke waarden spaties
	 * en komma's gebruikt kunnen worden (Bron: Hans-Georg Sobisch). Deze klasse maakt
	 * er regelmatig gefoprmatteerde, comma separated value files van. 
	 * 
	 * @param reader
	 * @throws IOException
	 */
	public void populateLithoClassesTable(BufferedReader reader)throws IOException{
		Pattern p = Pattern.compile(" ");
		Matcher m = null;
		boolean result = false;
		
		//Replace spaces by dashes (Lithoklasse file); separation by tabs remains, so
		//now  strings are separated from numerical values as within strings single 
		// spaces are used and between values tabs are used.
		line = reader.readLine();
		while (!(line == null || line.equals(""))){
			m = p.matcher(line);
			result = m.find();
			while(result){
				m.appendReplacement(sb, "-");
				result = m.find();
			}
			m.appendTail(sb);
			list.add(sb.toString());
			sb = new StringBuffer();
			line = reader.readLine();
		}
		// Determine number of rows  in list and number of elements in first row
		// to initialize LithoTable
		nrows = list.size();
		String firstRow = list.get(0);
		ncols = firstRow.split(TAB).length - 1;
		lithoTable = new String[ncols][nrows];			
		// Transfer list values to lithoTable
		int y=0;
		for (String newLine : list){
			String[] elements = newLine.split(TAB);
			for (int x = 0; x < ncols; x++){
				lithoTable[x][y] = elements[x];
			}
			y++;
		}
	}
	
	public int getNumberOfColumns(){
		return ncols;
	}
	
	public int getNumberOfRows(){
		return nrows;
	}
	
	public String[][] getLithoTable(){
		return lithoTable;
	}
	// export lithoTable to file
	public void writeLithoTable(PrintWriter writer){
		for(int j = 0; j < nrows; j++){
			String lithoLine = "";
			String separator = ",";
			for( int i = 0; i < ncols; i++){
				if(i == ncols - 1) separator = "";
				lithoLine = lithoLine + lithoTable[i][j] + separator;
			}
			writer.println(lithoLine);
			System.out.println(lithoLine);
		}
	}	
}
